home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Printing / PrintDialogMagic / PrintDialogMagic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-07  |  4.0 KB  |  144 lines  |  [TEXT/KAHL]

  1. /* PrintDialogMagic.c -- an example of how to print from an application
  2.     while still calling the print dialog (so that LW8.3 and such do the
  3.     right thing with the print record), but not requiring the user to
  4.     actually press any buttons or anything. This is most useful for apps
  5.     that want to do automated printing, such as a log to printer option
  6.     within some sort of server application.
  7.  
  8.     You may incorporate this sample code into your applications without
  9.     restriction, though the sample code has been provided "AS IS" and the
  10.     responsibility for its operation is 100% yours.  However, what you are
  11.     not permitted to do is to redistribute the source as "DSC Sample Code"
  12.     after having made changes. If you're going to re-distribute the source,
  13.     we require that you make it clear in the source that the code was
  14.     descended from Apple Sample Code, but that you've made changes.
  15. */
  16.  
  17. #include <Printing.h>
  18. #include <MixedMode.h>
  19. #include <SegLoad.h>
  20. #include <Resources.h>
  21. #include <ToolUtils.h>
  22. #include <Fonts.h>
  23.  
  24. /* Declare ‘pascal’ functions and procedures */
  25.  
  26. pascal    TPPrDlg MyJobDlgInit(THPrint);        /* Our extention to PrJobInit.        */
  27. OSErr    Print(void);
  28. pascal    Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  29.  
  30. static TPPrDlg PrtJobDialog;        /* pointer to job dialog */
  31.  
  32. static ModalFilterUPP    myFilterUPP;
  33.  
  34. /*------------------------------------------------------------------------*/
  35.  
  36. OSErr Print(void)
  37. {
  38.     TPPrPort    pPrPort;
  39.     Rect        aRect;
  40.     TPrStatus    theStatus;
  41.     THPrint        hPrintRec;
  42.     
  43.     
  44.     /* call PrJobInit to get pointer to the invisible job dialog */
  45.     hPrintRec = (THPrint)(NewHandle(sizeof(TPrint)));
  46.     PrintDefault(hPrintRec);
  47.     PrValidate(hPrintRec);
  48.     if (PrError() != noErr)
  49.         return PrError();        
  50.  
  51.     PrtJobDialog = PrJobInit(hPrintRec);
  52.        
  53.     if (PrError() != noErr)
  54.         return PrError();        
  55.  
  56.     if (!PrDlgMain(hPrintRec, NewPDlgInitProc(MyJobDlgInit))) /* this line does all the stuff */
  57.         return iPrAbort;
  58.  
  59.     if (PrError() != noErr)
  60.         return PrError();        
  61.  
  62.     pPrPort = PrOpenDoc(hPrintRec, NULL, NULL);
  63.     PrOpenPage(pPrPort, NULL);
  64.     
  65.     aRect = (*hPrintRec)->prInfo.rPage;
  66.     InsetRect(&aRect, 20, 20);
  67.     PenSize(4, 4);
  68.     FrameRect(&aRect);
  69.     
  70.     PrClosePage(pPrPort);
  71.     PrCloseDoc(pPrPort);
  72.  
  73.     if (!PrError() && (*hPrintRec)->prJob.bJDocLoop == bSpoolLoop)
  74.         PrPicFile(hPrintRec, NULL, NULL, NULL, &theStatus);
  75.  
  76.  
  77. /* that's all for now */
  78.         
  79.     if (hPrintRec) DisposeHandle((Handle) hPrintRec);
  80.     
  81.     return(noErr);
  82. } /* Print */
  83.  
  84. /*------------------------------------------------------------------------*/
  85.  
  86. pascal TPPrDlg MyJobDlgInit(THPrint hPrint)
  87. /*
  88.    this routine appends items to the standard job dialog and sets up the
  89.    user fields of the printing dialog record TPRDlg
  90.  
  91.    This routine will be called by PrDlgMain
  92. */
  93. {
  94. #pragma unused(hPrint)
  95.     /* we're going to be simple-minded about this, and just patch the
  96.     ** modal-filter proc for the dialog, and have it say that the user
  97.     ** actually hit the enter-key
  98.     
  99.     ** a more elaborate scheme would hide the dialog, patch ShowWindow
  100.     ** and/or ShowHide and patch out ModalDialog so that the dialog
  101.     ** never even appeared.
  102.     
  103.     ** Our method will briefly show the dialog, which will immediately
  104.     ** go away, which will blink the screen a little. Bummer.
  105.     */
  106.     
  107.     PrtJobDialog->pFltrProc = myFilterUPP;
  108.     
  109.     return PrtJobDialog;
  110. } /*myJobDlgInit*/
  111.  
  112. pascal    Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  113. {
  114. #pragma unused(theDialog)
  115. #pragma unused(theEvent)
  116.     *itemHit = 1;
  117.     return(true);
  118. }
  119.  
  120.  
  121. void main(void)
  122. {    
  123.     Rect        myWRect;
  124.     OSErr        err;
  125.     
  126.     InitGraf(&qd.thePort);
  127.     InitFonts();
  128.     InitWindows();
  129.     InitMenus();
  130.     InitDialogs((long)nil);
  131.     InitCursor();
  132.     SetRect(&myWRect,50,260,350,340);
  133.     
  134.     /* call the routine that does printing */
  135.     PrOpen();
  136.     /* build my UPP */
  137.     myFilterUPP = NewModalFilterProc(myFilter);
  138.     err = Print();
  139.     /* clean up */
  140.     DisposeRoutineDescriptor(myFilterUPP);
  141.     
  142.     PrClose();
  143. } /* main */
  144.